﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Windows.Forms;

public partial class manage_Product_update : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            getinfo();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string imageurl;
        imageurl = upimage1();
        string bookid = Request.Params["bookid"].ToString();
        DataClassesDataContext db = new DataClassesDataContext();
        var result = from a in db.tb_BookInfo
                     where a.BookID == int.Parse(bookid)
                     select a;
        foreach(tb_BookInfo info in result)
        {
            info.BookSyn=TextBox3.Text.ToString();
            info.BookName = TextBox1.Text.ToString();
            info.ClassID = int.Parse(DropDownList1.SelectedValue);
            info.HotPrice = int.Parse(TextBox2.Text);
            info.Discount = CheckBox3.Checked;
            info.Sell_well = CheckBox2.Checked;
            info.Recommend = CheckBox1.Checked;
            info.BookUrl = imageurl;
        }
        db.SubmitChanges();
        Response.Write("<script>alert('修改成功！')</script>");

    }
    public void getinfo()
    {
        string bookid = Request.Params["bookid"].ToString();

        string connstr = ConfigurationManager.ConnectionStrings["a123"].ToString();
        SqlConnection sqlconn = new SqlConnection(connstr);
        string sqlstr = "select * from tb_BookInfo  where  BookID='"+ bookid+"'";
        SqlDataAdapter myadpt = new SqlDataAdapter(sqlstr, sqlconn);
        DataSet ds=new DataSet();
        sqlconn.Open();
        myadpt.Fill(ds, "bookinfo");
        var a = ds.Tables["bookinfo"].Rows[0]["ClassID"];
        this.TextBox1.Text = ds.Tables["bookinfo"].Rows[0]["BookName"].ToString();
        DropDownList1.SelectedValue = ds.Tables["bookinfo"].Rows[0]["ClassID"].ToString();
        this.TextBox2.Text = ds.Tables["bookinfo"].Rows[0]["HotPrice"].ToString();
        this.TextBox3.Text = ds.Tables["bookinfo"].Rows[0]["BookSyn"].ToString();
        this.CheckBox1.Checked = bool.Parse(ds.Tables["bookinfo"].Rows[0]["Recommend"].ToString());
        this.CheckBox2.Checked = bool.Parse(ds.Tables["bookinfo"].Rows[0]["Sell_well"].ToString());
        this.CheckBox3.Checked = bool.Parse(ds.Tables["bookinfo"].Rows[0]["Discount"].ToString());
        sqlconn.Close();
    }
    protected string upimage1()
    {
        Boolean fileOK = false;
        string fileNama = "";
        //判断是否选中了文件
        if (FileUpload1.HasFile)
        {
            //获取上传文件扩展名
            String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
            //限定上传文件类型
            String[] allowedExtensions = { ".gif", ".png", ".jpg", ".bmp", ".jpeg", ".psd" };
            //判断文件类型是否符合要求
            for (int i = 0; i < allowedExtensions.Length; i++)
            {
                if (fileExtension == allowedExtensions[i])
                {
                    fileOK = true;
                }
            }
            if (fileOK)
            {
                try
                {   //获取文件名称
                    string filepath = Server.MapPath("~/product_pic/") + FileUpload1.FileName;
                    fileNama = "~\\product_pic\\" + FileUpload1.FileName;
                    this.FileUpload1.PostedFile.SaveAs(filepath);

                }
                catch (Exception ex)
                {
                    Response.Write("<script>alert('此文件格式不允许上传！！')</script>");

                }
            }
        }
        return fileNama;

    }
}